package cn.cqnu.controller.admin;
import cn.cqnu.domin.dto.AdminMatchListDto;
import cn.cqnu.domin.entity.Match;
import cn.cqnu.domin.vo.ResponseResult;
import cn.cqnu.global.SystemConstant;
import cn.cqnu.global.SystemReturnEnum;
import cn.cqnu.service.MatchService;
import cn.cqnu.utils.OSSUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;

@RestController
@RequestMapping("/admin/match")
public class AdminMatchController {

    @Autowired
    MatchService matchService;

    @PostMapping("/uploadBackImg")
    public ResponseResult uploadAvatar(MultipartFile file, String matchId, HttpServletRequest request) {

        if (file == null) {
            return ResponseResult.error(SystemReturnEnum.FILE_TYPE_ERR);
        } else {
            String originalFilename = file.getOriginalFilename();
            Path path = Paths.get(originalFilename);
            if (Files.isDirectory(path)) {
                return ResponseResult.error(SystemReturnEnum.FILE_TYPE_ERR);
            }
        }

        //获取文件的扩展名
        String temp = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".")).toLowerCase();

        //校验文件类型是否为图片
        if (!temp.equals(".jpg") && !temp.equals(".jpeg") && !temp.equals(".png")) {
            return ResponseResult.error(SystemReturnEnum.FILE_TYPE_ERR);
        }

        //设置背景图片文件名为时间戳
        long time = System.currentTimeMillis();
        String fileName = String.valueOf(time) + temp;

        try {
            String backImgUrl = OSSUtil.uploadFile(file.getInputStream(), SystemConstant.OSS_BACKGROUND_IMG_PATH, fileName);
            System.out.println(backImgUrl);

            matchService.saveImg(matchId, backImgUrl);
            return ResponseResult.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.error(SystemReturnEnum.FILE_TYPE_ERR);
        }
    }

    @PostMapping("/matchList")
    public ResponseResult searchMatchList(@RequestBody AdminMatchListDto matchInfo){
        HashMap<String, Object> result = matchService.adminMatchList(matchInfo);
        return ResponseResult.ok(result);
    }

    @PostMapping("/addMatch")
    public ResponseResult addMatch(@RequestBody Match matchInfo){
        return matchService.addMatch(matchInfo);
    }

    @PutMapping("/updateMatch")
    public ResponseResult updateMatch(@RequestBody Match matchInfo){
        matchService.updateMatch(matchInfo);
        return ResponseResult.ok();
    }
    @GetMapping("/getByid/{id}")
    public ResponseResult getById(@PathVariable Long id){
        return ResponseResult.ok(matchService.getById(id));
    }

    @DeleteMapping("/deleteMatch")
    public ResponseResult deleteMatch(@RequestParam String id){
        matchService.deleteMatch(id);
        return ResponseResult.ok();
    }
}

